//
//  ShareHeaderView.m
//  wtshoppingcite
//
//  Created by TW on 15-1-16.
//  Copyright (c) 2015年 TW. All rights reserved.
//

#import "ShareHeaderView.h"


@implementation ShareHeaderView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
         _imageViewArray = [[NSMutableArray alloc] initWithCapacity:0];
         [self createSubViews];
    }
    return self;
}


-(void)createSubViews{
    
    _scrollPanel = [[UIView alloc] initWithFrame:self.bounds];
    _scrollPanel.backgroundColor = [UIColor clearColor];
    _scrollPanel.alpha = 0;
    [self addSubview:_scrollPanel];
    
    _markView = [[UIView alloc] initWithFrame:_scrollPanel.bounds];
    _markView.backgroundColor = MAIN_BLACK_COLOR;
    _markView.alpha = 0.0;
    [_scrollPanel addSubview:_markView];
    
    _myScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    [_scrollPanel addSubview:_myScrollView];
    _myScrollView.pagingEnabled = YES;
    _myScrollView.delegate = self;
    CGSize contentSize = _myScrollView.contentSize;
    contentSize.height = self.bounds.size.height;
    contentSize.width = 320 * 3;
    _myScrollView.contentSize = contentSize;
    
    _iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
    _iconImageView.layer.cornerRadius = 20;
    _iconImageView.image = [UIImage imageNamed:@""];
    _iconImageView.clipsToBounds = YES;
    [self addSubview:_iconImageView];
    
    _nickNameLable = [self createLabelWithFrame:CGRectMake(_iconImageView.right +10, 5, 200, 30) text:@"" font:16 textAli:NSTextAlignmentLeft textColor:MAIN_BLACK_COLOR];
    [self addSubview:_nickNameLable];
    
    _levelImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 25, 25)];
    [self addSubview:_levelImageView];
    
    
    _dateLable = [self createLabelWithFrame:CGRectMake(_nickNameLable.left, _nickNameLable.bottom, 120, 18) text:@"" font:12 textAli:NSTextAlignmentLeft textColor:UIColorFromRGB(0x9fa0a0)];
    [self addSubview:_dateLable];
    
    _textLable = [self createLabelWithFrame:CGRectMake(5, _dateLable.bottom, kWidth-10, 80) text:@"" font:14 textAli:NSTextAlignmentLeft textColor:UIColorFromRGB(0x9fa0a0)];
    _textLable.numberOfLines = 0;
    [self addSubview:_textLable];
    
    
    if (_imageArray.count != 0) {
        
        [self initAdvScrollView];
    }else{
        CGRect bound=CGRectMake(0, _textLable.bottom, kWidth, 0);
        _backView=[[UIView alloc]initWithFrame:bound];
        
    }
    
    UIImageView* line = [[UIImageView alloc] initWithFrame:CGRectMake(0, _backView.bottom+2, SCREEN_WIDTH, 1)];
    line.backgroundColor = COLOR_RGB(222, 222, 222);
    line.tag = 232;
    [self addSubview:line];
    
    _customItemView = [[CustomSortButtonView alloc] initWithFrame:CGRectMake(0, line.bottom+1, SCREEN_WIDTH, 45)];
    _customItemView.tag = 303;
    _customItemView.delegate = self;
    [_customItemView initWithItemNames:@[@"",@""] itemImages:@[@"pinglun_gray",@"zuan_org"]];
    [self addSubview:_customItemView];
    
}

#pragma mark CustomSortButtonView -delegate
-(void)btnPressed:(NSInteger)sender{
   
    if (self.delegate && [self respondsToSelector:@selector(btnPressed:)]) {
        [self.delegate btnPressed:sender];
    }
}

-(void)initAdvScrollView{
    
    [_imageViewArray removeAllObjects];
    
    CGRect bound=CGRectMake(0, _textLable.bottom, kWidth, 120);
    _backView = [[UIView alloc]initWithFrame:bound];
    [self addSubview:_backView];
    
    for (int i = 0; i < _imageArray.count; i ++)
    {
       
        TapImageView *tmpView = [[TapImageView alloc] initWithFrame:CGRectMake(5+105*i, 10, 100, 100)];
        tmpView.t_delegate = self;
        tmpView.tag = 10 + i;
        
        NSString* urlstring = [NSString stringWithFormat:@"%@",self.imageArray[i]];
        urlstring = [urlstring stringByReplacingOccurrencesOfString:@" " withString:@""];
        [tmpView setImageWithURL:[NSURL URLWithString:urlstring] placeholderImage:[UIImage imageNamed:@"all_defalutImage"]];
        
        [_backView addSubview:tmpView];
        
    }
}

-(void)setShareContentModel:(ShareContentModel *)shareContentModel{
    if (_shareContentModel != shareContentModel) {
        _shareContentModel = shareContentModel;
        
        [_iconImageView setImageWithURL:[NSURL URLWithString: _shareContentModel.userIcon] placeholderImage:[UIImage imageNamed:@"touxiang_org"]];
       
        _nickNameLable.text = CHECK_ISNULL(_shareContentModel.nickName);
        _dateLable.text = shareContentModel.createTime;
        _textLable.text = shareContentModel.content;
        
        if(_shareContentModel.imgPaths.count == 0){
            }
        
        NSString *commentNum ;
        commentNum = CHECK_IS_NULL(shareContentModel.commentNumber);
        NSString *commentSum = [NSString stringWithFormat:@"%@评论",commentNum];
        
        NSString *parisenum ;
        
        NSMutableDictionary* dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"PariseAddKey"];
        parisenum = [dic objectForKey:shareContentModel.typeId];
        
        if(parisenum == nil || [parisenum isEqual:[NSNull null]] || [parisenum isEqualToString:@"(null)"]){
            parisenum = @"0";
        }
        NSString *praiseNum = [NSString stringWithFormat:@"%@点赞",parisenum];

        [_customItemView setItemNames:@[commentSum,praiseNum]];
        
        self.imageArray = shareContentModel.imgPaths;

        CGFloat height = [UIUtility getHeightOfString: shareContentModel.content withFont:[UIFont systemFontOfSize:14.0f] withWidth:kWidth-10];
        _textLable.height =height+5;
        
        if (self.imageArray.count != 0) {
            [self initAdvScrollView];
            
            UIImageView* line = (UIImageView*)[self viewWithTag:232];
            line.frame = CGRectMake(0, _backView.bottom+2, SCREEN_WIDTH, 1);
            _customItemView.frame = CGRectMake(0, line.bottom+1, SCREEN_WIDTH, 45);
            
        }else{
            
            UIImageView* line = (UIImageView*)[self viewWithTag:232];
            line.frame = CGRectMake(0, _textLable.bottom+2, SCREEN_WIDTH, 1);
            _customItemView.frame = CGRectMake(0, line.bottom+1, SCREEN_WIDTH, 45);
        
        }

    }
}

-(void)customItemViewWithArray:(NSString*)shareId{

    NSString *commentNum ;
    if (_shareContentModel.commentNumber ==nil) {
        commentNum = @"0";
    }else{
        commentNum = _shareContentModel.commentNumber;
    }
    NSString *commentSum = [NSString stringWithFormat:@"%@评论",commentNum];
    
    NSString *parisenum ;
//    int p = _shareContentModel.praiseNumber.intValue;
    
    NSMutableDictionary* dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"PariseAddKey"];
    if(dic == nil){
        parisenum = @"0";
    }else{
        parisenum = [dic objectForKey:shareId];
    }
    NSString *praiseNum = [NSString stringWithFormat:@"%@点赞",parisenum];
    
    [_customItemView setItemNames:@[commentSum,praiseNum]];
}



#pragma mark -
#pragma mark - custom delegate
- (void) tappedWithObject:(id)sender
{
    if (self.delegate && [self respondsToSelector:@selector(tappedWithObject:)]) {
        [self.delegate tappedWithObject:sender];
    }
    
}


-(UILabel*)createLabelWithFrame:(CGRect)frame text:(NSString*)text font:(int)font textAli:(NSTextAlignment)alig textColor:(UIColor*)tc{
    if(text == nil)
        text = @"";
    UILabel* label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = alig;
    label.textColor = tc;
    label.numberOfLines = 0;
    label.backgroundColor = [UIColor clearColor];
    return label;
}

@end
